﻿// See https://aka.ms/new-console-template for more information
using Newtonsoft.Json;
using S5_DBAccess;
using System.Data;
using System.Data.SqlClient;
////数据库连接串
//string connectString = "Server=.\\SQLEXPRESS;DataBase=tz1989_fw;Trusted_Connection=true;";
////查询表数据的select
//string select_sql = @"SELECT [Id]
//      ,[Name]
//      ,[Gender]
//      ,[Age]
//      ,[CreateTime]
//      ,[UpdateTime]
//  FROM [dbo].[base_user]
//  Where Id='001'
//";
//#region 隐藏
////插入数据的insert
//string insert_sql = @$"INSERT INTO [dbo].[base_user]
//           ([Id]
//           ,[Name]
//           ,[Gender]
//           ,[Age]
//           ,[CreateTime]
//           ,[UpdateTime])
//     VALUES
//           ('003'
//           ,'小刚'
//           ,1
//           ,13
//           ,'{DateTime.Now}'
//           ,null)";

//string update_sql = @$"UPDATE [dbo].[base_user]
//   SET [Age] = 15
//      ,[UpdateTime] = '{DateTime.Now}'
// WHERE Id='003'";

//string delete_sql = @"DELETE FROM [dbo].[base_user]
//      WHERE Id='003'";
//#endregion
//DataTable dataTable = new DataTable();

////数据库访问连接对象
//SqlConnection conn = new SqlConnection(connectString);
////数据库访问的命令对象
//SqlCommand cmd = conn.CreateCommand();
//cmd.CommandText = select_sql;
////cmd.CommandText = delete_sql;
//cmd.CommandType = System.Data.CommandType.Text;
////打开数据库
//conn.Open();
//////数据库查找器
//SqlDataAdapter adapter = new SqlDataAdapter(cmd);
//////通过执行sql语句查找数据，并填充到DataTable对象中
//adapter.Fill(dataTable);
////执行操作sql
////int row=cmd.ExecuteNonQuery();
//conn.Close();

////Console.WriteLine(row);
///

//DataTable dataTable = QueryUser("001' or 1=1 --");

//string json = JsonConvert.SerializeObject(dataTable);
//Console.WriteLine(json);



DBHelper.SetConnectionString("Server=.\\SQLEXPRESS;DataBase=tz1989_fw;Trusted_Connection=true;");
int row = AddUser();
Console.WriteLine(row);


//DataTable dt = QueryUser("001");
//string json = JsonConvert.SerializeObject(dt);
//Console.WriteLine(json);

Console.ReadLine();

static DataTable QueryUser(string id)
{
    //查询表数据的select
    string select_sql = @$"SELECT [Id]
      ,[Name]
      ,[Gender]
      ,[Age]
      ,[CreateTime]
      ,[UpdateTime]
  FROM [dbo].[base_user]  
  Where Id=@Id
";

    DataTable dt = DBHelper.SqlQuery(select_sql, new SqlParameter[] {
    new SqlParameter("@Id", id)
});
    return dt;
}

static int AddUser()
{
    ////插入数据的insert
    string insert_sql = @$"INSERT INTO [dbo].[base_user]
               ([Id]
               ,[Name]
               ,[Gender]
               ,[Age]
               ,[CreateTime]
               ,[UpdateTime])
         VALUES
               ('009'
               ,'小丽'
               ,0
               ,14
               ,'{DateTime.Now}'
               ,null);";
    int row = DBHelper.ExecuteSql(insert_sql);
    return row;
}

